//
//  MahjongRecordCell.m
//  Mahjong
//
//  Created by 饶首建 on 17/5/15.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "MahjongRecordCell.h"
#import "PlayerCell.h"
#import "ControlButton.h"
#import "HWPopTool.h"

#import "ResultModel.h"
#import "ActionModel.h"

@interface MahjongRecordCell()  <UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

static NSString *kPlayerCell = @"PlayerCell";

@implementation MahjongRecordCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PlayerCell class]) bundle:nil] forCellReuseIdentifier:kPlayerCell];
    _tableView.delegate = self;
    _tableView.dataSource = self;
}

- (IBAction)recordAction:(id)sender {
    if (_recordBlock) {
        _recordBlock();
    }
}
- (IBAction)detailAction:(id)sender {
    if (_detailBlock) {
        _detailBlock();
    }
}

- (void)setGameModel:(GameModel *)gameModel{
    _gameModel = gameModel;
    [_tableView reloadData];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *playerDicArr = _gameModel.resultArr;
    return playerDicArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PlayerCell *cell = [tableView dequeueReusableCellWithIdentifier:kPlayerCell forIndexPath:indexPath];
    
    ResultModel *rm = _gameModel.resultArr[indexPath.row];
    
    cell.nameLabel.text = rm.playerModel.playerName;
    cell.bonusLabel.text = NSStringFormat(@"%ld",rm.score);
    
    if ([rm.playerModel.playerId isEqual:_owner.playerId]) {
        cell.nameLabel.textColor = [UIColor redColor];
        cell.bonusLabel.textColor = [UIColor redColor];
    }else{
        cell.nameLabel.textColor = [UIColor blackColor];
        cell.bonusLabel.textColor = [UIColor blackColor];
    }
    
    NSMutableArray *attrDisplayArr = [NSMutableArray new];
    NSMutableArray *attrDisplayActionId = [NSMutableArray new];
    for (ActionModel *am in _gameModel.actionArr) {
        if ([am.owner.playerId isEqualToString:rm.playerModel.playerId]) {
            [attrDisplayArr addObject:am.displayName];
            [attrDisplayActionId addObject:am.actionId];
        }
    }
    
    for (UIView *v in cell.controlContainerView.subviews) {
        [v removeFromSuperview];
    }
    
    float x = 0;
    for (int i=0;i<attrDisplayArr.count;i++) {
        NSString *title = attrDisplayArr[i];
        ControlButton *btn;
        CGRect frame;
        if (attrDisplayArr.count > 6) {
            btn = [[ControlButton alloc]initWithTitle:title fontSize:10];
            frame = btn.frame;
            frame.size.width = frame.size.width + 10;
        }else{
            btn = [[ControlButton alloc]initWithTitle:title fontSize:14];
            frame = btn.frame;
            frame.size.width = frame.size.width + 20;
        }
        frame.origin.y = (cell.controlContainerView.frame.size.height - frame.size.height)/2;
        frame.origin.x = CGRectGetMaxX(cell.controlContainerView.subviews.lastObject.frame)+5;
        btn.frame = frame;
        x+= 5 + frame.size.width;
        
        //标签点击事件:只能改自己的
        if ([rm.playerModel.playerId isEqual:_owner.playerId]) {
            
            btn.layer.borderColor = [UIColor redColor].CGColor;
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

            [btn addTapBlock:^(UIButton *btn) {
                for (ActionModel *am in _gameModel.actionArr) {
                        if ([title isEqualToString:am.displayName] && [am.actionId isEqualToString:attrDisplayActionId[i]]) {
                            if (_tagClickBlock) {
                                _tagClickBlock(am);
                            }
                            break;
                        }
                }
            }];
            
        }
        
        [cell.controlContainerView addSubview:btn];
        [cell.controlContainerView setContentSize:CGSizeMake(x, 42)];
    }
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 42.5f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 弹框
- (void)showModal{
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 210)];
    contentView.backgroundColor = [UIColor whiteColor];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 200, 40)];
    label1.text = NSStringFormat(@"赢 玩家A +15 下雨*2 平胡");
    label1.textColor = [UIColor grayColor];
    label1.font = DefaultSystemFont(14);
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(20, 70, 200, 40)];
    label2.text = NSStringFormat(@"赢 玩家B +15 下雨*2 平胡");
    label2.textColor = [UIColor grayColor];
    label2.font = DefaultSystemFont(14);
    
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 120, 200, 40)];
    label3.text = NSStringFormat(@"输 玩家C -2 对对胡");
    label3.textColor = [UIColor grayColor];
    label3.font = DefaultSystemFont(14);
    
    [contentView addSubview:label1];
    [contentView addSubview:label2];
    [contentView addSubview:label3];
    
    [HWPopTool sharedInstance].shadeBackgroundType = ShadeBackgroundTypeGradient;
    [HWPopTool sharedInstance].closeButtonType = ButtonPositionTypeRight;
    [[HWPopTool sharedInstance] showWithPresentView:contentView animated:NO];
}

#pragma mark - private method


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
